package controllers;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import models.Report;
import models.Setting;
import play.mvc.Result;
import services.ReportService;
import services.SettingService;
import services.SynchronizationService;
import views.html.reports.list;

public class Reports extends play.mvc.Controller {
	
	private static final SettingService settingService = SettingService.getService();
	private static final SynchronizationService synchService = SynchronizationService.getService();
	private static final ReportService reportService = ReportService.getService();
	
	public static Result synchronizeReports() {
		List<Report> reports = synchService.synchronizeReports(settingService.getSetting(Setting.TEST_REPORT_HOME).getStringValue());
		Collections.sort(reports, new Comparator<Report>() {
			@Override
			public int compare(Report r1, Report r2) {
				return r2.getGeneratedTime().compareTo(r1.getGeneratedTime());
			}
		});
		return ok(list.render(reports));
	}
	
	public static Result showReport(String projectId, Long reportId) {
		return ok(new File(reportService.getReportHtmlPath(projectId, reportId.toString(), settingService.getSetting(Setting.TEST_REPORT_HOME).getStringValue())));
	}
	
}
